
import java.io.IOException;
import java.util.Random;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author khang
 */
public class Enemy1 {
    private int iwidth,iheight;
    private Sprite Enemy,power,death;
    private int health,havePower,isdeath;
    private Sound sound;
    public Enemy1(LayerManager lm,Sound s,int width,int height){
        sound=s;
        iwidth=width;
        iheight=height;
        health=2;
        isdeath=0;
        try {
            Enemy=new Sprite(Image.createImage("/Cirno.png"), 32, 32);
            //Enemy.setFrame(8);
            //int[]frameSequence={0,1,2,3};
            //Enemy.setFrameSequence(frameSequence);
            Enemy.defineReferencePixel(Enemy.getWidth()/2, Enemy.getHeight()/2);
            Enemy.setVisible(false);
            power=new Sprite(Image.createImage("/DevildomTorchOrrerriesSun.png"), 32, 32);
            power.setFrame(3);           
            power.defineReferencePixel(power.getWidth()/2, power.getHeight()/2);
            power.setVisible(false);
            death=new Sprite(Image.createImage("/DevildomTorchOrrerriesSun.png"), 32, 32);
            int[]frameSequence2={0,1,2};
            death.setFrameSequence(frameSequence2);
            death.defineReferencePixel(death.getWidth()/2, death.getHeight()/2);
            death.setVisible(false);
            lm.append(Enemy);
            lm.append(power);
            lm.append(death);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    public void setPosition(int x,int y){
        Enemy.setVisible(true);
        Enemy.setRefPixelPosition(x, y);
    }
    public Sprite getEnemy(){
        return Enemy;
    }
    //di chuyen xuong tu 2 phia
    public void moveEnemyType1(int index, int scrY){
        if(index%2==0){
            Enemy.move(1, 3);
            //Enemy.nextFrame();
        }
        else{
            Enemy.move(-1, 3);
            //Enemy.nextFrame();
        }
        if(Enemy.getRefPixelY()-Enemy.getHeight()/2>scrY+iheight){
            Enemy.setVisible(false);
        }
    }
    //di chuyen sang ngang tu 2 phia
    public void moveEnemyType2(int index, int scrY){
        if(index%2==0){
            Enemy.move(3, 2);
            //Enemy.nextFrame();
        }
        else{
            Enemy.move(-3, 2);
            //Enemy.nextFrame();
        }
        if(Enemy.getRefPixelY()-Enemy.getHeight()/2>scrY+iheight){
            Enemy.setVisible(false);
        }
    }
    //di chuyen cheo xuong sang phai
    public void moveEnemyType3(int scrY){
        Enemy.move(1, 3);
        //Enemy.nextFrame();
        if(Enemy.getRefPixelY()-Enemy.getHeight()/2>scrY+iheight){
            Enemy.setVisible(false);
        }
    }
    //di chuyen cheo xuong sang trai
    public void moveEnemyType4(int scrY){
        Enemy.move(-1, 3);
        //Enemy.nextFrame();
        if(Enemy.getRefPixelY()-Enemy.getHeight()/2>scrY+iheight){
            Enemy.setVisible(false);
        }
    }
    public int getHealth(){
        return health;
    }
    public void setHealth(int health){
        this.health=health;
    }
    public void setCondition(){
        int iX=Enemy.getRefPixelX();
        int iY=Enemy.getRefPixelY();
        if(health<=0){
            Random ran=new Random();
            havePower=ran.nextInt(2);
            Enemy.setVisible(false);
            isdeath=2;
            health=2;
            death.setRefPixelPosition(iX, iY);
            death.setVisible(true);
            if(havePower==1){
                power.setRefPixelPosition(iX, iY);
                power.setVisible(true);
            }
            //sound.enemyDeathSound();
        }
        if(isdeath>=0){          
            death.nextFrame();
            isdeath--;
        }
        else{
            death.setVisible(false);
        }
    }
    public int checkFire(Sprite sprite,int health){
        if(Enemy.collidesWith(sprite, true)){
            Enemy.setVisible(false);
            health--;
        }
        return health;
    }
    public int checkPower(Sprite sprite,int pow,int scrY){
        if(power.collidesWith(sprite, true)){
            power.setVisible(false);
            pow++;
        }
        if(power.getRefPixelY()>scrY+iheight){
            power.setVisible(false);
        }
        return pow;
    }
    public int getPosition(){
        return Enemy.getRefPixelY();
    }
    public void refreshEnemy(){
        Enemy.setVisible(false);
        power.setVisible(false);
        death.setVisible(false);
    }
}
